package com.mycompany.javafxmavenproject;

import chartUtils.ChartSupport;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import mymath.Functions;
import mymath.Functions.MY_FUNCTION;

public class MainApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        stage.setTitle("JavaFX with Maven");

        Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));
        AnchorPane page = (AnchorPane) root;

        System.out.println(FXMLLoader.FX_CONTROLLER_ATTRIBUTE);

        Pane plotPane = (Pane) page.lookup("#AnchorPane1");

        if (plotPane == null) {
            System.out.println("AnchorPane1 is null!");
        } else {
            System.out.println("AnchorPane1 is not null!");
        }

        LineChart<Number, Number> lineChart = createLineChart();
        
        VBox lineChartInVBoxLayout = ChartSupport.createVBoxLayoutForChart(lineChart);

        Label cursorCoords = ChartSupport.createCursorGraphCoordsMonitorLabel(lineChart);

        lineChartInVBoxLayout = ChartSupport.addCursorCoords(lineChartInVBoxLayout, cursorCoords);
        
        plotPane.getChildren().add(lineChartInVBoxLayout);
        plotPane.getStyleClass().add("pane");

        Scene scene = new Scene(page);
        stage.setScene(scene);
        scene.getStylesheets().add("/styles/Styles.css");

        stage.show();

    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    private LineChart<Number, Number> createLineChart() {
        //defining the axes
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();

        //FIXME: manual formatting of XAxis
        xAxis.setAutoRanging(true);
        //xAxis.setLowerBound(3);
        //xAxis.setUpperBound(8);
        xAxis.setTickUnit(1);
        //to disable the minor ticks for the horizontal axis
        xAxis.setMinorTickCount(0);

        final LineChart<Number, Number> lineChart
                = new LineChart<>(xAxis, yAxis);
        lineChart.setTitle("Function plot");
        Functions f = new Functions();
        XYChart.Series series1 = f.getSeriesFx(MY_FUNCTION.FX1);
        series1.setName("Fx1");
        XYChart.Series series2 = f.getSeriesFx(MY_FUNCTION.FX2);
        series2.setName("Fx2");
        lineChart.getData().addAll(series1, series2);

        return lineChart;
    }

}
